import { useBusProduct } from '/@/stores/busProduct';
import { getProductCateList, getProductAttriList,getProductAttriValueList } from '/@/api/main/busProduct';

const stores = useBusProduct();

// 获取物料属性
export const getProductAttrListFn = async (productCode: any) => {
  const { data } = await getProductAttriValueList({ p1: productCode });
  const list = data.result ?? [];
  // console.log(list,"product");
  if (list.length) {
    // stores.productAttrList = list.map((item: { value: any; }) => ({ value: item.value }));
    stores.productAttrList = list.map((item: {aValueId:any, value: any }) => ( { value: item.value,  aValueId: item.aValueId } ));
  }
}

// 获取物料分类
export const getProductCateLstFn = async (productCode: any) => {
  const { data } = await getProductCateList({ p1: productCode });
  const list = data.result ?? [];
  if (list.length) {
    stores.productCateRowList = list.map((item: { value: any; }) => ({ value: item.value }));
  }
}

// 物料属性
export const productAttributeEffect = () => {
  // 增加物料属性
  const addProductAttributeRow = () => stores.setProductAttrList({});

  // 删除物料属性
  const deleteProductAttributeRow = (k: number) => stores.delProductAttrList(k, 1);

  return {
    addProductAttributeRow,
    deleteProductAttributeRow
  }
}

// 物料分类
export const productCateRowEffect = () => {
  // 增加物料分类
  const addProductCateRow = () => stores.setProductCateRowList({});

  // 删除物料分类
  const deleteProductCateRow = (k: number) => stores.delProductCateRowList(k, 1);

  return {
    addProductCateRow,
    deleteProductCateRow
  }
}